﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data.SqlClient;
using LotteryVote.Utility;

namespace LotteryVote.Data
{
    public class LoginLogDataAccess : DataBase
    {
        public void AddLog(User user)
        {
            string sql = string.Format(@"INSERT INTO {0} ({1},{2}) VALUES (@{1},@{2}) SELECT IDENT_CURRENT('{0}')", LoginLog.TABLENAME, LoginLog.USERID, LoginLog.IPFIELD);
            base.ExecuteNonQuery(sql, new SqlParameter(LoginLog.USERID, user.UserId),
                new SqlParameter(LoginLog.IPFIELD, IPHelper.IPAddress));
        }

        public IList<LoginLog> ListTopLog(User user, int count)
        {
            string sql = string.Format(@"SELECT TOP {3} * FROM {0} WHERE {1}=@{1} ORDER BY {2} DESC", LoginLog.TABLENAME, LoginLog.USERID, LoginLog.LOGINID, count);
            return base.ExecuteList<LoginLog>(sql, new SqlParameter(LoginLog.USERID, user.UserId));
        }
    }
}
